package org.dreamwork.jasmine2.web.html;

import org.dreamwork.jasmine2.engine.HttpContext;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.i18n.I18nUtil;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.util.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-10-30
 * Time: 11:03:52
 */
public class ClientScriptControl extends GenericHtmlControl {
    private boolean i18n;

    public ClientScriptControl () {
        super ("script");
    }

    public boolean isI18n () {
        return i18n;
    }

    public void setI18n (boolean i18n) {
        this.i18n = i18n;
    }

    @Override
    public void onPrerender (IWebControl sender, HttpContext context) throws EventException {
        String src = getAttribute ("src");
        if (i18n && !StringUtil.isEmpty (src)) {
            I18nUtil.replaceAttribute ("src", this);
        }

        if (text == null) return;
        String content = text.toString ();
        if (!StringUtil.isEmpty (content)) {
            if (i18n && content.contains ("${"))
                content = I18nUtil.eval (content, this);
            text = I18nUtil.escapeI18n (content);
        }
    }
}